﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.TestItemForms
{
    public partial class TestItemMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.TestItemBll TestItemBll = new BLL.TestItemBll();
        public TestItemMainForm()
        {
            InitializeComponent();
        }

        private void TestItemMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            InitGrid();
        }

        private void InitGrid()
        {
            var models = TestItemBll.LoadEntitiesAsNoTracking(m => true).OrderByDescending(m => m.Order).ToList();
            this.gridControl1.DataSource = models;
        }

        private void ButtonItem_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TestItemAddForm testItemAddForm = new TestItemAddForm(InitGrid);
            testItemAddForm.Show();
        }
        private bool AddTestItem(TestItem testItem)
        {
            //盘重
            int count = TestItemBll.LoadEntitiesAsNoTracking(m => m.ChineseName == testItem.ChineseName).Count();
            if (count > 0)
            {
                XtraMessageBox.Show(ResourceOverall.DataExists);
                return false;
            }

            var res = TestItemBll.AddEntity(testItem);
            if (res != null)
            {
                InitGrid();
                XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                return true;
            }
            else
            {
                XtraMessageBox.Show(ResourceOverall.CreateFailed);
                return false;
            }

        }

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (OJCB4Entities db =new OJCB4Entities())
            {
                if (this.gridView1.GetFocusedRowCellValue("TestItemID") == null)
                {
                    XtraMessageBox.Show(ResourceOverall.SelectNull);
                    return;
                }
                string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("TestItemID"));
                var testitem = db.TestItems.Find(id);
                int count = new BLL.ToolBll().GetReportCountByTestItem(testitem);
                if (count > 0)
                {
                    XtraMessageBox.Show(ResourceOverall.DeleteExists);
                    return;
                }
                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    db.TestItems.Remove(testitem);
                    bool res = db.SaveChanges() > 0;
                    if (res)
                    {
                        InitGrid();
                        XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                    }
                } 
            }
        }

        private void ButtonItem_edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("TestItemID")!=null)
            {
                string id = Convert.ToString( this.gridView1.GetFocusedRowCellValue("TestItemID"));
                TestItem testItem = TestItemBll.LoadEntities(m => m.TestItemID == id).FirstOrDefault();
                if (testItem!=null)
                {
                    DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmEdit, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        TestItemAddForm from = new TestItemAddForm(testItem, InitGrid);
                        from.ShowDialog();
                    }
                }
            }
            
        }
    }
}